<script setup lang="ts" name="Dept">
import editPage from "./edit.vue";
import detailPage from "./detail.vue";
import { ROOT_ID } from "@/views/system/dept/const";
import { delDeptApi, listDeptApi } from "@/api/system/dept";

const { proxy } = getCurrentInstance();

const { sys_normal_disable } = useDict("sys_normal_disable")

// 遮罩层
const loading = ref(false);

// 导出遮罩层
const exportLoading = ref(false);

// 搜索栏元素
const queryDeptRef = ref(null);

// 表单Id
const id = ref(null);

// 上级部门ID
const parentId = ref(null);

// 选中数组
const selection = ref<DeptRecord[]>([])

// 全部展开
const isExpandAll = ref(true);

// 表格刷新
const refreshTable = ref(true);

// 部门管理表数据
const deptList = ref<DeptRecord[]>([]);

// 弹出层标题
const title = ref("");

// 编辑弹出层
const openEdit = ref(false);

// 详情弹出层
const openDetail = ref(false);

// 列信息
const columns = ref<TableColumnsRecord>({
  deptName: { label: "部门名称", visible: true },
  orderNum: { label: "排序", visible: true },
  leader: { label: "负责人", visible: true },
  phone: { label: "联系电话", visible: true },
  email: { label: "邮箱", visible: true },
  status: { label: "部门状态", visible: true },
})

// 查询参数
const queryParams = ref<UserRecord>({
  deptId: null,
  parentId: null,
  deptName: null,
  orderNum: null,
  leader: null,
  phone: null,
  email: null,
  status: null,
});

/** 查询部门管理列表 */
function getList() {
  loading.value = true;
  listDeptApi(queryParams.value).then(response => {
    deptList.value = handleTree(response.data, "deptId");
    loading.value = false;
  });
}

/** 展开/折叠操作 */
function toggleExpandAll() {
  refreshTable.value = false;
  isExpandAll.value = !isExpandAll.value;
  nextTick(() => {
    refreshTable.value = true;
  });
}

/** 关闭弹窗 */
function closeDialog(refresh: boolean) {
  if (refresh) {
    getList();
  }
  openEdit.value = false;
  openDetail.value = false;
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryDeptRef.value.resetFields()
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd(row: DeptRecord) {
  if (row.deptId) {
    parentId.value = row.deptId;
  } else {
    parentId.value = null;
  }
  id.value = null;
  openEdit.value = true;
  title.value = "新增部门管理";
}

/** 修改按钮操作 */
function handleUpdate(row: DeptRecord) {
  id.value = row.deptId || selection.value.map(item => item.deptId);
  openEdit.value = true;
  title.value = "修改部门管理";
}

/** 详情按钮操作 */
function handleDetails(row: DeptRecord) {
  id.value = row.deptId;
  openDetail.value = true;
  title.value = "部门管理详情";
}

/** 删除按钮操作 */
function handleDelete(row: DeptRecord | any) {
  let ids = [];
  let deptNames = [];
  if (row.deptId) {
    ids = [row.deptId];
    deptNames = [row.deptName];
  } else {
    selection.value.forEach(item => {
      ids.push(item.deptId);
      deptNames.push(item.deptName);
    })
  }

  // @ts-ignore
  proxy.$modal.confirm('是否确认删除名称为"' + deptNames + '"的数据项?', "系统提示", {
    type: "warning",
  }).then(() => {
    return delDeptApi(ids);
  }).then(() => {
    getList();
    // @ts-ignore
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

getList();
</script>

<template>
  <div class="app-container">
    <div class="back-item">
      <el-form :model="queryParams" ref="queryDeptRef" :inline="true" label-width="68px">
        <el-form-item label="部门名称" prop="deptName">
          <el-input
              v-model="queryParams.deptName"
              placeholder="请输入部门名称"
              clearable
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="部门状态" prop="status">
          <el-select
              v-model="queryParams.status"
              placeholder="请选择部门状态"
              clearable
              @keyup.enter.native="handleQuery"
          >
            <el-option
                v-for="(item, index) in sys_normal_disable"
                :key="index"
                :label="item.label"
                :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item class="item-search">
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button type="warning" icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="back-item">
      <div class="table-top mb8">
        <div class="title title-left">
          部门管理列表
        </div>
        <el-button
            type="info"
            plain
            icon="Sort"
            @click="toggleExpandAll"
        >展开/折叠
        </el-button>
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-has-permi="['system:dept:add']"
        >新增
        </el-button>
        <span class="table-top-line" />
        <right-toolbar @queryTable="getList" :columns="columns" />
      </div>
      <el-table
          v-if="refreshTable"
          v-loading="loading"
          :data="deptList"
          row-key="deptId"
          v-tablesticky
          :default-expand-all="isExpandAll"
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column label="部门名称" prop="deptName" width="160" v-if="columns.deptName.visible" />
        <el-table-column label="排序" prop="orderNum" align="center" v-if="columns.orderNum.visible" />
        <el-table-column label="负责人" prop="leader" align="center" v-if="columns.leader.visible" />
        <el-table-column label="联系电话" prop="phone" align="center" min-width="120" v-if="columns.phone.visible" />
        <el-table-column label="邮箱" prop="email" align="center" min-width="120" v-if="columns.email.visible" />
        <el-table-column label="部门状态" prop="status" align="center" v-if="columns.status.visible">
          <template #default="scope">
            <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="140">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button
                  link
                  type="primary"
                  icon="Edit"
                  @click="handleUpdate(scope.row)"
                  v-has-permi="['system:dept:edit']"
              />
            </el-tooltip>
            <el-tooltip content="新增" placement="top">
              <el-button
                  link
                  type="primary"
                  icon="Plus"
                  @click="handleAdd(scope.row)"
                  v-hasPermi="['system:dept:add']"
              />
            </el-tooltip>
            <el-tooltip content="详情" placement="top">
              <el-button
                  link
                  type="primary"
                  icon="Document"
                  @click="handleDetails(scope.row)"
                  v-has-permi="['system:dept:query']"
              />
            </el-tooltip>
            <el-tooltip content="删除" placement="top" v-if="scope.row.parentId != ROOT_ID">
              <el-button
                  link
                  type="primary"
                  icon="Delete"
                  @click="handleDelete(scope.row)"
                  class="text-danger"
                  v-has-permi="['system:dept:remove']"
              />
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <div class="line-magin" />
    </div>

    <!-- 添加或修改部门管理对话框 -->
    <edit-page v-if="openEdit" :id="id" :parent-id="parentId" :title="title" @closeDialog="closeDialog" />
    <detail-page v-if="openDetail" :id="id" :title="title" @closeDialog="closeDialog" />
  </div>
</template>

<style lang="scss" scoped>

</style>